package br.cns;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import br.cns.metrics.SingleFailureImpact;
import br.cns.models.ErdosRenyiP;

public class SingleFailureTest {
	
	@Test
	public void testSmallRing() {
		Integer[][] net = new Integer[][]{
				{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
				{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
				{0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
				{0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
				{0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0},
				{0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0},
				{0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0},
				{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0},
				{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}};
		
		SingleFailureImpact sfi = SingleFailureImpact.getInstance();
		double value = sfi.calculate(net);
		System.out.println("testSmallRing = " + value);
		assertEquals(value, 0.1667, 4);
	}
	
	@Test
	public void testBigRing() {
		Integer[][] net = new Integer[200][200];
		
		for (int i = 0; i < net.length; i++){
			if (i == 0){
				net[i][net.length-1] = 1;	
			}else{
				net[i][i-1] = 1;
			}
			if (i == net.length-1){
				net[i][0] = 1;
			}else{
				net[i][i+1] = 1;
			}
		}
		
		SingleFailureImpact sfi = SingleFailureImpact.getInstance();
		double value = sfi.calculate(net);
		System.out.println("testBigRing = " + value);
		assertEquals(value, 0.1667, 4);
	}

	@Test
	public void testFinlandia() {
		Integer[][] net = new Integer[][]{
				{0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0},
				{0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
				{0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0},
				{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
				{0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0},
				{0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1},
				{0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1},
				{0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1},
				{0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1},
				{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}};
		
		SingleFailureImpact sfi = SingleFailureImpact.getInstance();
		double value = sfi.calculate(net);
		System.out.println("testFinlandia = " + value);
		assertEquals(value, 0.1667, 4);
	}
	
	@Test
	public void testStar() {
		Integer[][] net = new Integer[][]{
				{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
				{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
		
		SingleFailureImpact sfi = SingleFailureImpact.getInstance();
		double value = sfi.calculate(net);
		System.out.println("testStar = " + value);
		assertEquals(value, 1, 0);
	}
	
	@Test
	public void testSmallRandom() {
		Integer[][] net = new Integer[20][20];
		ErdosRenyiP erdos = new ErdosRenyiP(0.2);
		net = erdos.transform(net);
		
		SingleFailureImpact sfi = SingleFailureImpact.getInstance();
		double value = sfi.calculate(net);
		System.out.println("testSmallRandom = " + value);
		assertEquals(value < 0.3, true);
	}
	
	@Test
	public void testBigRandom() {
		Integer[][] net = new Integer[200][200];
		ErdosRenyiP erdos = new ErdosRenyiP(0.2);
		net = erdos.transform(net);
		
		SingleFailureImpact sfi = SingleFailureImpact.getInstance();
		double value = sfi.calculate(net);
		System.out.println("testBigRandom = " + value);
		assertEquals(value < 0.05, true);
	}
	
	@Test
	public void testBigStar() {
		Integer[][] net = new Integer[200][200];
		
		for (int i = 0; i < net.length; i++){
			net[0][i] = 1;
			net[i][0] = 1;
		}
		
		SingleFailureImpact sfi = SingleFailureImpact.getInstance();
		double value = sfi.calculate(net);
		System.out.println("testBigStar = " + value);
		assertEquals(value, 1, 0);
	}

}
